package view;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.sql.*;
import java.util.*;

import model.Color;
import control.CColor;

public class NewColor extends JPanel {
  public static final String NAME = "color";
  private SnakesCRUD parent;
  
  private CColor controlColor;
  private ArrayList<Color> colors;
  
  public NewColor(SnakesCRUD parent) {
    this.parent = parent;
    initComponents();
    
    controlColor = new CColor(this.parent.getConnection());
    colors = new ArrayList<Color>();
    
    updateTable();
  }
  
  public final void updateTable() {
    try {
	  this.colors = controlColor.retrieveAll();
		        
	  Object[] o = new Object[2];
	  ((DefaultTableModel)(jTable1.getModel())).setRowCount(0);
		        
	  for (int i = 0; i < colors.size(); i++) {
	    o[0] = colors.get(i).getColorID();
	    o[1] = colors.get(i).getDescription();
		          
        ((DefaultTableModel)(jTable1.getModel())).addRow(o);
      }
    }
    catch(Exception e) {}
  }

  
  private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        edID = new javax.swing.JTextField();
        edDescription = new javax.swing.JTextField();
        btnAdd = new javax.swing.JButton();
        btnRemove = new javax.swing.JButton();
        btnModify = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        jLabel1.setText("ID:");

        jLabel2.setText("Description:");

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnRemove.setText("Remove");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });

        btnModify.setText("Modify");
        btnModify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModifyActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setText("Editing COLOR Table");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "ID", "Description"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Refresh");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(edDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(edID, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel3)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(btnAdd)
                            .addGap(47, 47, 47)
                            .addComponent(btnRemove)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnModify)
                            .addGap(43, 43, 43)
                            .addComponent(jButton1))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(47, 47, 47))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(edID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(edDescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnRemove)
                    .addComponent(jButton1)
                    .addComponent(btnModify))
                .addGap(26, 26, 26))
        );
    }// </editor-fold>//GEN-END:initComponents

  private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
    if (!this.edID.getText().isEmpty() && !this.edDescription.getText().isEmpty()) {
      try {
        Color c = new Color(Integer.parseInt(this.edID.getText()), this.edDescription.getText());
    
        controlColor.create(c);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data added!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch (SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }//GEN-LAST:event_btnAddActionPerformed

  private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
    if (!this.edID.getText().isEmpty()) {
      try {
        Color c = new Color(Integer.parseInt(this.edID.getText()), this.edDescription.getText());
    
        controlColor.delete(c);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data deleted!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }//GEN-LAST:event_btnRemoveActionPerformed

  private void btnModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModifyActionPerformed
    if (!this.edID.getText().isEmpty() && !this.edDescription.getText().isEmpty()) {
      try {
        Color c = new Color(Integer.parseInt(this.edID.getText()), this.edDescription.getText());
    
        controlColor.update(c);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data modified!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }
  
  private javax.swing.JButton btnAdd;
  private javax.swing.JButton btnModify;
  private javax.swing.JButton btnRemove;
  private javax.swing.JTextField edDescription;
  private javax.swing.JTextField edID;
  private javax.swing.JButton jButton1;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JTable jTable1;
}
